24. 两两交换链表中的节点
为保证权益,题目请参考 24. 两两交换链表中的节点(From LeetCode).
解决方案1
Python
python
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def swapPairs(self, head: ListNode) -> ListNode:
if head is None:
return None
if head.next is None:
return head
t = head.next
head.next = t.next
t.next = head
head.next = self.swapPairs(head.next)
return t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22